package com.tangdi.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.tangdi.ReportFileInfo;
import com.tangdi.Utils;
import com.tangdi.UtilsPrintPDF;
import com.tangdi.Bean.DW_DM_MC;
import com.tangdi.Dao.impl.GongziDWImpl;

public class dwGZ_MX_TableAction  extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(dwGZ_MX_TableAction.class);
	public dwGZ_MX_TableAction() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String urlJsp = "dwGZ_MX_TableShow.jsp";
		String type = request.getParameter("type");
		String year_month = request.getParameter("year_month");
		String folder_Name_mx = ReportFileInfo.FOLDER_NAME_GZMX.getName();
		String file_Name_mx = ReportFileInfo.FILE_NAME_GZMX.getName();

		logger.info(folder_Name_mx);

		HttpSession session=request.getSession();
		
		if(year_month != null && type.equals("2")){
			List<DW_DM_MC> dw_dm_mcList = new ArrayList<DW_DM_MC>();
			GongziDWImpl gzdwmx = new GongziDWImpl();
			dw_dm_mcList = gzdwmx.getDWInfo(year_month);
			 
			session.setAttribute("dw_dm_mcList",dw_dm_mcList);
			session.setAttribute("year_month",year_month);
			session.setAttribute("folder_Name_mx",folder_Name_mx);
			session.setAttribute("file_Name_mx",file_Name_mx);
			response.sendRedirect(urlJsp);
			
		}else if(type.equals("3")){//打印
			String msg = "success";
			PrintWriter out=response.getWriter();
			String filePathString = request.getParameter("file_paths");
			String[] filePaths = filePathString.substring(0, filePathString.length() - 1).split(",");
			for(int i = 0; i < filePaths.length; i++){
				filePaths[i] = Utils.getFilePath("root_path") + filePaths[i];
				//ReportFileInfo.ROOT_PATH.getName() + filePaths[i];
				logger.info(filePaths[i]);
			}
			String print_result = "";
			try{
				print_result = UtilsPrintPDF.printPDF(filePaths);
			}catch(Exception e){
				e.printStackTrace();
				msg = "Failure";
				out.print(msg);
				out.flush();
				out.close();
			}
			msg = print_result;
			
			logger.info("msg:" + msg);
			out.print(msg);
			out.flush();
			out.close();
			
		}else{
			session.setAttribute("dw_dm_mcList",null);
			session.setAttribute("year_month",null);
			session.setAttribute("folder_Name_mx",null);
			session.setAttribute("file_Name_mx",null);
			response.sendRedirect(urlJsp);
			
		}
	}
}
